package com.zcl.springboot.hello.controller;

import com.zcl.springboot.hello.bean.User;
import com.zcl.springboot.hello.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
    public class HelloController {
        private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HelloController.class);

        @Autowired
        HelloService helloService;
        @Autowired
        private RedisTemplate<Object,Object> redisTemplate;

        @RequestMapping("/index")
        @ResponseBody
        public String sayHello() {
            return "12345";
        }

        @RequestMapping("/getUserInfo")
        public String getUserInfo() {
            String name = "zcl";
            String password = "123456";
            log.info("name"+name +"password"+password);
            User userInfo = helloService.getUserInfo(name, password);
            if (userInfo==null) {
            }
            return "index";
        }

    @RequestMapping("/getUserById")
    public String getUserById(@RequestParam  Integer id,@RequestParam String name) {
        log.info("id:"+id+"  name:"+name);
        //先查缓存
        User user = (User) redisTemplate.opsForValue().get(name);
        User userInfo = null;
        if (user==null){
            userInfo = helloService.getUserInfo("zcl", "123456");
            redisTemplate.opsForValue().set(name,userInfo);
        }
        if (userInfo==null) {
            return "error";
        }
        return "index";
    }

    }
